<?php
require_once(dirname(__FILE__) . '/../../includes/spip_launcher.php');
include_spip('functions/pdk_common');
require_once(_DIR_PLUGIN_PDK.'classes/simpletest/class.PdkTest.php');

class TestClassEcatalogue extends PdkTest {
    /**
     * Ecatalogue
     */
    private $ecatalogue;
	
    
    /**
     * ProductModel
     */
    private $product_mdl;
    
    /**
     * PdkRubriqueModel
     */
    private $rubrique_mdl;
    
    /**
     * PdkArticleModel
     */ 
    private $article_mdl;
    
    
    
    public function __construct(){
        parent::__construct();
        
        // Define all additional models:
        $pdk_models = array();
        $this->product_mdl = pdk_load_class('ProductModel','classes');
        $pdk_models['ProductModel'] = $this->product_mdl;
        $this->rubrique_mdl = pdk_load_class('PdkRubriqueModel','classes/models');
        $pdk_models['PdkRubriqueModel'] = $this->rubrique_mdl;        
        $this->article_mdl = pdk_load_class('PdkArticleModel','classes/models');
        $pdk_models['PdkArticleModel'] = $this->article_mdl;                
        $this->AddPdkModels($pdk_models);
		
		$this->ecatalogue = pdk_load_class('Ecatalogue','classes');
        
        $this->dropTestTables = ECATALOGUE_TESTS_DROP_TEST_TABLES;
		
		// Remember ecatalogue rubriques
		$this->ecatalogue_branches = lire_config('ecatalogue/branches');
    }
    
    /**
     *  Run before each test
     */
    function setUp() {
       parent::setUp();
    }
    
    /**
     *  Run after each test
     */
    function tearDown() {
       parent::tearDown();
    }
    
    function __destruct() {       
       parent::__destruct();
	   // Return ecatalogue rubriques
	   ecrire_config('ecatalogue/branches', $this->ecatalogue_branches);
    }
         
    function TestPerformMaintenance_DeleteForgottenProducts() {		
		$this->article_mdl->delete_by_id(1);
        $this->article_mdl->delete_by_id(2);
		
		$this->ecatalogue->PerformMaintenance();
		
		$this->assertFalse($this->product_mdl->select_by_id(1));
		$this->assertFalse($this->product_mdl->select_by_id(2));
    }
	
	function TestPerformMaintenance_AddProductsForArticles() {
		// Make first rubrique ecatalogue's rubrique, and place articles in it
		ecrire_config('ecatalogue/branches', 'rubrique|1');
		$this->article_mdl->update(false, array('id_rubrique'=>1));
		// Delete products
		$this->product_mdl->delete();
		
		$this->ecatalogue->PerformMaintenance();
		
		$this->assertTrue($this->product_mdl->select_by_id(1));
		$this->assertTrue($this->product_mdl->select_by_id(2));
	}
}
